package com.bjpowernode.oa.bean;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSessionBindingEvent;
import jakarta.servlet.http.HttpSessionBindingListener;

public class User implements HttpSessionBindingListener {
    private String username;
    private String password;

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        //用户登录了
        ServletContext application = event.getSession().getServletContext();
        Object onlinecount = application.getAttribute("onlinecount");
        if(onlinecount==null){
            application.setAttribute("onlinecount",1);
        }else{
            int count=(Integer)onlinecount;
            application.setAttribute("onlinecount",count+1);
        }
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        //用户退出了
        ServletContext application = event.getSession().getServletContext();
        Object onlinecount = application.getAttribute("onlinecount");
        int count=(Integer)onlinecount;
        application.setAttribute("onlinecount",count-1);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User() {
    }
}
